layui.use(['jquery', 'tinymce','upload','element','treeSelect','form','tag','transfer'], function() {
    var $ = layui.jquery;
    var element = layui.element;
    var tinymce = layui.tinymce
    var treeSelect = layui.treeSelect;
    var form = layui.form;
    var upload = layui.upload;
    var tag = layui.tag;
    var mykeywords = new Array();//存储文章关键词
    var mytags = new Array();//存储文章标签

    //标签的监听
    tag.on('click(tags)', function(data) {});
    tag.on('add(tags)', function(data) {});
    tag.on('delete(tags)', function(data) {});
    //关键词tag监听
    tag.on('click(keywordstags)', function(data) {});
    tag.on('add(keywordstags)', function(data) {});
    tag.on('delete(keywordstags)', function(data) {});

    //富文本编辑器
    var edit = tinymce.render({
        elem: "#edit"
        , height: 680
        , width:'100%'
    });

    var layid = location.hash.replace(/^#article=/, '');
    element.on('tab(article)', function(){
        location.hash = 'article='+ this.getAttribute('lay-id');
    });

    //拖拽上传
    upload.render({
        elem: '#image_upload'
        , url: '/admin.php/upload'
        ,accept: 'image'
        ,size: 4 * 1024
        , done: function (res) {
            layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.data);
            layui.$("#article_image_cover").val(res.data);
        }
    });

    //加载下拉树
    treeSelect.render({
        // 选择器
        elem: '#tree',
        //请求类型
        type:'post',
        // 数据
        data: '/admin.php/articlecategory/tree',
        // 占位符
        placeholder: '默认顶级分类',
        // 是否开启搜索功能：true/false
        search: true,
        // 点击回调
        click: function(d){
            $("#tree").val(d.current.id)
        }
    });

    //获取自动标签监听
    $("#autoGetTags").click(function (){
        var article_content = edit.getContent({format:'text'})
        var article_title = $("#article_title").val();
        $.ajax({
            type:'post',
            url: "/admin.php/article/articleAnalyseTags",
            data:{'article_content':article_content,'article_title':article_title},
            success:function(result){
                var i = 0;
                if(result.code==1){
                    (result.data).forEach(function(element) {
                        tag.add('tags', {
                            text: element,
                            id:element
                        })
                    });
                }
            }
        })
    })

    //获取自动关键词监听
    $("#autoGetKeywords").click(function (){
        var article_content = edit.getContent({format:'text'})
        $.ajax({
            type:'post',
            url: "/admin.php/article/articleAnalyseKeywords",
            data:{'article_content':article_content},
            success:function(result){
                if(result.code==1){
                    (result.data).forEach(function(element) {
                        tag.add('keywordstags', {
                            text: element,
                            id: element //实际使用一般是规定好的id，这里以时间戳模拟下
                        })
                    });
                }
            }
        })
    })
    //获取SEO描述
    $("#autoSEODescription").click(function (){
        console.log('des')
        var article_content = edit.getContent({format:'text'})
        $.ajax({
            type:'post',
            url: "/admin.php/article/articleAnalyseAbstarct",
            data:{'article_content':article_content},
            success:function(result){
                $("#seo_description").val(result.data);
            }
        })
    })

    //获取SEO关键字
    $("#autoSEOKeywords").click(function (){
        console.log('key')
        var article_content = edit.getContent({format:'text'})
        $.ajax({
            type:'post',
            url: "/admin.php/article/articleAnalyseKeywords",
            data:{'article_content':article_content},
            success:function(result){
                $('#seo_keywords').val(result.data.join(','));
            }
        })
    })

    //提交数据
    form.on('submit(save)', function(data){
        //封装关键词数据
        var children = $("#keywordstags-content").children("button");
        for (var i=0; i< children.length-1;i++){
            mykeywords[i] = children[i].getAttribute('lay-id');
        }

        //封装标签数据
        var children = $("#tags-content").children("button");
        for (var i=0; i< children.length-1;i++){
            mytags[i] = children[i].getAttribute('lay-id');
        }
        //封装status数据
        if (data.field.status=='on'){
            data.field.status = 1;
        }else{
            data.field.status = 0;
        }
        //添加数据
        data.field.mykeywords = mykeywords;
        data.field.mytags = mytags;
        data.field.article_content = edit.getContent()
        data.field.article_text = edit.getContent({format:'text'})

        $.ajax({
            type:'post',
            url: "/admin.php/article/add",
            data:data.field,
            success:function(result){
                console.log(result);
                // if(result.code==1){
                //     var index = parent.layer.getFrameIndex(window.name);
                //     parent.layer.close(index);
                // }else{
                //     notice.error(result.msg)
                // }
            }
        })
        return false;
    });

});